<template>
    <view class="container">
        <view class="order_list" v-if="orderList&&orderList.length!=0">
            <view class="order_item" v-for="(item,index) in orderList" :key="index" @click="getBelongOrderDataFun(listPageTwo,item.id,'init')">
              <view class="item_info">
                  <view class="item_info_left">
                      <view class="item_info_options">
                          <image class="item_info_options_icon" src="https://image1.sansancloud.com/xianhua/2020_6/8/17/18/1_861.jpg?x-oss-process=style/preview_120"></image>
                          <text class="item_info_options_text">{{item.invoiceNo}}</text>
                      </view>
                      <view class="item_info_options">
                          <image class="item_info_options_icon" src="https://image1.sansancloud.com/xianhua/2020_6/8/18/3/10_466.jpg?x-oss-process=style/preview_120"></image>
                          <text class="item_info_options_text">{{item.perTime}} {{item.weekOne}} {{item.endTime}}</text>
                      </view>
                  </view>
                  <view class="item_info_right">
                      <view class="item_price_block">
                          <text class="item_price" :style="{color: defaultColor}">
                              {{item.requestAmount}}
                          </text>
                          <text class="item_price_unit">元</text>
                      </view>
                  </view>
              </view>
              <view class="item_bottom_block">
                  <view class="item_state">
                      <text v-if="item.invoiceFlag==2">已开票</text>
                      <text v-if="item.invoiceFlag==1">申请中</text>
                      <text v-if="item.invoiceFlag==3">已拒绝</text>
                  </view>
                  <view class="check_belong_order_btn" @click="getBelongOrderDataFun(listPageTwo,item.id,'init')">
                      <text>查看所属订单</text>
                  </view>
              </view>
            </view>
        </view>
        <view class="noproduct" v-else>
          <image mode="widthFix" src='https://image1.sansancloud.com/xianhua/2019_12/16/15/54/55_950.jpg?x-oss-process=style/preview_120'></image>
          <text>您还没开票历史哦~</text>
        </view>
        <!-- 所属订单 -->
        <view class='zhezhao' v-if="showNoneBill" @tap='closeZhezhao'></view>
        <view class="non_payment_list" :animation="animationData">
          <view class="non_payment_title">
            <text>订单列表</text>
          </view>
          <scroll-view v-if="curBelongOrderList.length!=0&&showNoneBill" scroll-y  style='height:800rpx' lower-threshold='5' @scrolltolower="scrollBottomToLoadMore">
          <view class="non_payment_item" v-for="(item, index) in curBelongOrderList" :key="index" :data-url="'order_detail.html?orderNo='+item.orderNo" @click="tolinkUrl">
            <view class="non_payment_item_left">
              <text class="non_payment_item_name">订单号：{{item.orderNo}}</text>
              <text class="non_payment_item_time">下单时间：{{item.perTime}} {{item.weekOne}} {{item.endTime}}</text>
            </view>
            <view class="non_payment_item_price">
              <text>￥{{item.endPrice}}</text>
            </view>
          </view>
          </scroll-view>
          <view class="no_bill" v-else>
            <text>暂无订单</text>
          </view>
        </view>
    </view>
</template>

<script>
import { socketFun } from "../../wxcomponents/public/json2Form";
export default {
  data() {
    return {
      orderList:null,
      setting:null,
      listPage:{
          page: 1,
          pageSize: 20,
          totalSize: 0,
      },
      listPageTwo:{
          page: 1,
          pageSize: 20,
          totalSize: 0,
          invoiceId:0
      },
      secondColor:'',
      defaultColor:'',
      curBelongOrderList:[],
      showNoneBill:false,
      animationData:null,
    };
  },
  props: {},

  /*onload*/
  onLoad: function (options) {
    let that=this;
    console.log("======onLoad:options======", options, getApp().globalData.setting);
    setTimeout(function(){
        that.getData(that.listPage,'upload');
    },100)
  },
  onReady: function () {
    
  },
  onShow: function () {
    let that=this;
	that.setData({
        setting:getApp().globalData.setting,
        defaultColor:getApp().globalData.setting.platformSetting.defaultColor,
        secondColor:getApp().globalData.setting.platformSetting.secondColor,
       })
  },
  /* 分享 app.js862行*/
  onShareAppMessage: function () {
    console.log(getApp().globalData.miniIndexPage);
    return getApp().globalData.shareForFx2(getApp().globalData.miniIndexPage);
  },
  onPullDownRefresh: function () {
    let that = this; 
    console.log("下拉");
    that.listPage.page=1
    that.getData(that.listPage);
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('===onReachBottom====');
    let that = this
    if (that.listPage.totalSize > that.listPage.page * that.listPage.pageSize) {
      that.listPage.page++
      that.getData(that.listPage);
    }else{
        wx.showToast({
          title: "到底了~",
          image: '/wxcomponents/images/icons/tip.png',
          duration: 1000
        });
    }
  },
  methods: {
    scrollBottomToLoadMore:function(){
        console.log('===scrollBottomToLoadMore====');
        let that = this
        if (that.listPageTwo.totalSize > that.listPageTwo.page * that.listPageTwo.pageSize) {
          that.listPageTwo.page++
          that.getBelongOrderDataFun(that.listPageTwo);
        }else{
            wx.showToast({
              title: "到底了~",
              image: '/wxcomponents/images/icons/tip.png',
              duration: 1000
            });
        }
    },
    closeZhezhao:function(){
        let that=this;
        that.showNoneBill=false;
        let animation = wx.createAnimation({
          duration: 400,
          timingFunction: 'ease',
        })
        animation.bottom('-200rpx').step()
        let setData = animation.export()
        that.setData({
          animationData: setData,
        })
    },
    showBelongOrderDataBanFun:function(){
        let that=this;
        console.log("===showBelongOrderDataBanFun====",)
        let showNoneBill=!that.showNoneBill;
        that.setData({
          showNoneBill: showNoneBill,
        })
        let animation = wx.createAnimation({
          duration: 400,
          timingFunction: 'ease',
        })
        if (showNoneBill) {
          animation.bottom('0rpx').step()
        } else {
          animation.bottom('-200rpx').step()
        }
        that.setData({
          animationData: animation.export(),
        })
    },
    /* 获取所属订单数据 */
    getBelongOrderDataFun: function (param,id,state) {
      var that = this
      let params=Object.assign({},param,{
          invoiceId:id||that.listPageTwo.invoiceId
      })
      if(state=='init'){
        that.listPageTwo.invoiceId=id
      }
      var customIndex = getApp().globalData.AddClientUrl("/wx_find_orders_by_invoice.html", params)
      getApp().globalData.showToastLoading('loading', true)
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          wx.hideLoading()
          console.log(res.data);
          if(res.data.errcode==0){
              if(state&&state=='init'){
                that.showBelongOrderDataBanFun()
              }
              let data=res.data.relateObj.result
              that.listPageTwo.pageSize = res.data.relateObj.pageSize
              that.listPageTwo.page = res.data.relateObj.curPage
              that.listPageTwo.totalSize = res.data.relateObj.totalSize
              let dataArr = that.curBelongOrderList;
              for (let i = 0; i < data.length; i++) {
                data[i].endPrice=(data[i].totalPayedAmount-data[i].backAmount).toFixed(2)
                if(data[i].addTime){
                  let addTime=data[i].addTime.replace(/\-/g, "/"); 
                  data[i].perTime=data[i].addTime.replace(/\-/g, ".").slice(0,10);
                  data[i].endTime=data[i].addTime.slice(11,-3);
                  let dateTime = new Date(addTime);
                  let dayNum=dateTime.getDay();
                  switch (dayNum) {
                    case 0:
                        data[i].weekOne='周日'
                      break;
                    case 1:
                        data[i].weekOne='周一'
                      break;
                    case 2:
                        data[i].weekOne='周二'
                      break;
                    case 3:
                        data[i].weekOne='周三'
                      break;
                    case 4:
                        data[i].weekOne='周四'
                      break;
                    case 5:
                        data[i].weekOne='周五'
                      break;
                    case 6:
                        data[i].weekOne='周六'
                      break;
                  }
                }
              }
              console.log("===data====",data)
              if (param.page == 1) {
                dataArr = []
              }
              if (!data || data.length == 0) {
                that.setData({ curBelongOrderList: [] })
              } else {
                dataArr = dataArr.concat(data)
                that.setData({ curBelongOrderList: dataArr })
              }
          }else{
              that.listPageTwo.page--
          }
          wx.hideLoading()
        },
        fail: function (res) {
          console.log("fail")
          wx.hideLoading()
          getApp().globalData.loadFail()
        }
      })
    },
    /* 获取数据 */
    getData: function (param,state) {
      var that = this
      console.log("===param==",param)
      var customIndex = getApp().globalData.AddClientUrl("/wx_find_invoice_list.html", param)
      getApp().globalData.showToastLoading('loading', true)
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          wx.hideLoading()
          console.log(res.data)
          let data=res.data.relateObj.result
          that.listPage.pageSize = res.data.relateObj.pageSize
          that.listPage.page = res.data.relateObj.curPage
          that.listPage.totalSize = res.data.relateObj.totalSize
          let dataArr = that.orderList;
          for (let i = 0; i < data.length; i++) {
            that.$set(data[i],'state',false);
            data[i].endPrice=(data[i].totalPayedAmount-data[i].backAmount).toFixed(2)
            if(data[i].requestDatetime){
              let requestDatetime=data[i].requestDatetime.replace(/\-/g, "/"); 
              data[i].perTime=data[i].requestDatetime.replace(/\-/g, ".").slice(0,10);
              data[i].endTime=data[i].requestDatetime.slice(11,-3);
              let dateTime = new Date(requestDatetime);
              let dayNum=dateTime.getDay();
              switch (dayNum) {
                case 0:
                    data[i].weekOne='周日'
                  break;
                case 1:
                    data[i].weekOne='周一'
                  break;
                case 2:
                    data[i].weekOne='周二'
                  break;
                case 3:
                    data[i].weekOne='周三'
                  break;
                case 4:
                    data[i].weekOne='周四'
                  break;
                case 5:
                    data[i].weekOne='周五'
                  break;
                case 6:
                    data[i].weekOne='周六'
                  break;
              }
            }
          }
          console.log("===data====",data)
          if (param.page == 1) {
            dataArr = []
          }
          if (!data || data.length == 0) {
            that.setData({ orderList: [] })
          } else {
            dataArr = dataArr.concat(data)
            that.setData({ orderList: dataArr })
          }
          wx.hideLoading()
        },
        fail: function (res) {
          console.log("fail")
          wx.hideLoading()
          getApp().globalData.loadFail()
        }
      })
    },
    /* 组件事件集合 */
    tolinkUrl: function (e) {
      console.warn("=======e=======", e);
      let linkUrl = e.currentTarget.dataset.url;
      getApp().globalData.linkEvent(linkUrl);
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>